package worker;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.*;

import bookshopSM.Logon;

public class UpdateCode extends JPanel {
    JButton bwrite,bclear;
    JPasswordField ptold,ptnew1,ptnew2;
    JLabel lold,lnew1,lnew2,ltitle;
    static  Connection conn = null;
    private static String username ="root";
    private static String password="root" ;
    private static String driverClassName="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.1:3307/books_system";
    int wnoupdate;
    public UpdateCode(){
        String personID = Logon.getStf();
        wnoupdate=Integer.valueOf(personID);
        ltitle=new JLabel("修改密码       ");
        ltitle.setFont(new java.awt.Font("楷体",1,20));
        lold=new JLabel( "原  密  码：");
        lnew1=new JLabel("新  密  码：");
        lnew2=new JLabel("确认密码：");
        ptold=new JPasswordField(7);
        ptnew1=new JPasswordField(7);
        ptnew2=new JPasswordField(7);
        bwrite=new JButton("确认修改");
        bclear=new JButton("取消");
        Box p1=Box.createHorizontalBox();
        Box p2=Box.createHorizontalBox();
        Box p3=Box.createHorizontalBox();
        Box p4=Box.createHorizontalBox();
        Box box1=Box.createVerticalBox();
        p1.add(lold);
        p2.add(lnew1);
        p3.add(lnew2);
        p1.add(ptold);
        p2.add(ptnew1);
        p3.add(ptnew2);
        p4.add(bwrite);
        p4.add(bclear);
        box1.add(Box.createVerticalStrut(15));
        box1.add(ltitle);
        box1.add(Box.createVerticalStrut(15));
        box1.add(p1);
        box1.add(Box.createVerticalStrut(10));
        box1.add(p2);
        box1.add(Box.createVerticalStrut(10));
        box1.add(p3);
        box1.add(Box.createVerticalStrut(10));
        box1.add(p4);
        this.add(box1);
        bwrite.addActionListener(
                new ActionListener() {

                    public void actionPerformed( ActionEvent event )
                    {
                        UpdateRecord();
                    }
                }
        );

        bclear.addActionListener(
                new ActionListener() {

                    public void actionPerformed( ActionEvent event )
                    {
                        clearRecord();
                    }
                }
        );
    }
    @SuppressWarnings("deprecation")
    protected void UpdateRecord() {
        // TODO Auto-generated method stub
        try{
            //Class.forName(driverClassName); //或者Class.forName(driverClassName)...newInstance();
            conn = DriverManager.getConnection(url,username,password);
            Statement stmt=conn.createStatement();
            System.out.print(wnoupdate);
            ResultSet rs=stmt.executeQuery("select Wcode from Worker "+
                    " where Wno="+ wnoupdate);
            rs.next();
            if(String.valueOf(ptold.getPassword()).equals(rs.getString("Wcode"))){
                if(String.valueOf(ptnew1.getPassword()).equals(String.valueOf(ptnew2.getPassword()))){

                    try{
                        stmt.executeUpdate("update Worker set Wcode='"+
                                String.valueOf(ptnew1.getPassword())+"'"+ " where wno="+wnoupdate);
                        JOptionPane.showMessageDialog(null, "修改成功");
                    }
                    catch (Exception ee){
                        System.out.println(ee);
                    }
                    finally{
                        if(conn!=null){
                            try {
                                conn.close();
                            } catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            conn=null;
                        }
                    }
                }
                else{
                    JOptionPane.showMessageDialog(null, "两次输入密码不一致");
                }
            }
            else{

                JOptionPane.showMessageDialog(null, "原密码不正确");
            }
        }
        catch (Exception ee){
            System.out.println(ee+"as");
        }
        finally{
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn=null;
            }
        }

    }
    protected void clearRecord() {
        // TODO Auto-generated method stub
        ptold.setText("");
        ptnew1.setText("");
        ptnew2.setText("");
    }
}
